package com.cy6;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author CY
 * @date 2022/10/19 13:22
 **/
public class MyHeap {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 10;

    public MyHeap(int[] elem, int usedSize) {
        this.elem = elem;
        this.usedSize = usedSize;
    }

    public void creatHeap() {
        for (int parent = (usedSize - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(parent, usedSize);
        }
    }

    public void shiftDown(int parent, int len) {
        int child = 2 * parent + 1;
        while (child < len) {
            if (child + 1 < len && elem[child] < elem[child + 1]) {
                child++;
            }
            if (elem[parent] < elem[child]) {
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public void offer(int val) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, 2 * usedSize);
        }
        this.elem[usedSize] = val;
        usedSize++;
        shiftUp(usedSize - 1);
    }

    public void shiftUp(int child) {
        int parent = (child - 1) / 2;
        while (child > 0) {
            if (elem[child] > elem[parent]) {
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }

        }
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    public void pop() {
        if (isEmpty()) {
            return;
        }
        int tmp = elem[0];
        elem[0] = elem[usedSize - 1];
        elem[usedSize - 1] = tmp;
        shiftDown(0, usedSize--);
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

    public int[] smallestK(int[] arr, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            pq.offer(arr[i]);
        }
        for (int i = k; i < pq.size(); i++) {
            if (pq.peek() > arr[i]) {
                pq.poll();
                pq.offer(arr[i]);
            }
        }
        while(!pq.isEmpty()){
            ret[k--]=pq.poll();
        }
        return ret;
    }

    public static void main(String[] args) {
        //测试自己实现的堆
        int[] arr = new int[]{27, 15, 19, 18, 28, 34, 65, 49, 25, 37};
        MyHeap myHeap = new MyHeap(arr, arr.length);
        myHeap.creatHeap();
        myHeap.offer(188);
        myHeap.pop();
        System.out.println();
        //java中的堆
        PriorityQueue<Integer> pq = new PriorityQueue<>();

    }


}
